/**
 * @Title: warning_ctrl.js
 * @Description: TODO
 * Copyright: Copyright (c) 2014
 * Company:中国平安
 *
 * @Project HIMS-FWA
 * @date 2014年10月27日 下午4:43:38
 * @version V1.0
 */
;
(function(){
    'use strict';

    var app = angular.module('fwa.mainApp');
    app.controller('CancelDrugStoreIntgrityCtrl', ['$scope','CommunityService','DrugStoreService','UpdateDrugStoreService','$state','BaseURL','Modal','FwaTabsetService','$stateParams',
        function($scope,CommunityService,DrugStoreService,UpdateDrugStoreService,$state,baseUrl,Modal,fwaTabsetService,$stateParams){

    		//页面
    		$scope.includeUrl = 'views/hcpms/evaluation/evaluationDrugStore/drugStoreIntgrity/cancelDrugStoreIntgrityInclude.html';

            $scope.intgrityEvalTypeInfo={};
            //tab切换事件
            $scope.tabClick = function(data) {
                $scope.intgrityEvalTypeInfo = data;
                $scope.selectedTab = data;
                loadIntgrityEvalDetailInfo(data.evaluRuleId,data.intgrityEvalChildId);
             }
            //初始化请求参数
            var evaluationCode=null;
            if($stateParams.evaluationCode){
                evaluationCode = $stateParams.evaluationCode;
            }
            /**
             * 初始化诚信单位考评详情
             */
            $scope.intgrityEvalData = {};
            function loadIntgrityEvalData() {
                var parameter={evaluationCode:evaluationCode};
                UpdateDrugStoreService.loadIntgrityEvalInfo(parameter).success(function(response){
                   if(response.state){
                       $scope.intgrityEvalData = response.result.intgrityEvalInfo;
                  }
               });
            }
            /**
             * 初始化诚信考评类别详情
             */
            $scope.tabs = [];
            function loadIntgrityEvalTypeInfo() {
                var parameter = {evaluationCode:evaluationCode};
                UpdateDrugStoreService.loadIntgrityEvalTypeInfo(parameter).success(function(response){
                    if(response.state){
                    	$scope.tabs = response.result.intgrityEvalTypeInfo;
                    	console.log($scope.tabs);
                    	 //第一次默认选中第一个tab页
                        $scope.tabClick($scope.tabs[0]);
                    }
                });
            }
            
            var intgrityEvalMap = {};
            /**
             * 初始化诚信考评详情
             */
            $scope.intgrityEvalInfo = {};
            function loadIntgrityEvalDetailInfo(evaluRuleId,intgrityEvalChildId) {
                var parameter= {evaluRuleId:evaluRuleId,intgrityEvalChildId:intgrityEvalChildId};
                if (!intgrityEvalMap[evaluRuleId] || intgrityEvalMap[evaluRuleId].isFlag) {
                	$scope.loading=true;
                	UpdateDrugStoreService.loadIntgrityEvalDetailInfo(parameter).success(function(response){
                		$scope.loading=false;
                        if (response.state) {
                            $scope.intgrityEvalInfo = response.result.data;
                            intgrityEvalMap[evaluRuleId] = response.result.data;
                            intgrityEvalMap[evaluRuleId].isFlag = false;
                            getTableDatas(intgrityEvalMap[evaluRuleId]);
                        }
                    });
                }
                else {
                	$scope.intgrityEvalInfo = intgrityEvalMap[evaluRuleId];
                	getTableDatas(intgrityEvalMap[evaluRuleId]);
                }
                
            }

            //json 数据重新组合
            function getTableDatas(datas) {
                if (datas) {
                    var tableDatas = [];
                    angular.forEach(datas,function(oneData,i) {
                        var oneLength = 0;
                        oneData.no = i + 1;
                        tableDatas.push(oneData);
                        angular.forEach(oneData.list,function(twoData,j) {
                            twoData.rowSpan = twoData.list.length;
                            if (j == 0) {
                                oneData.data = twoData;
                            }
                            else {
                                twoData.oneHide = true;
                                tableDatas.push(twoData);
                            }
                            if (twoData.list && twoData.list.length > 0 ) {
                                angular.forEach(twoData.list,function(data,k) {
                                    oneLength++;
                                    if (data.isConfirm == '0') {
                                        data.checked = true;
                                    }
                                    else {
                                        data.checked = true;
                                    }
                                    if (k == 0) {
                                        twoData.data = data;
                                    }
                                    else {
                                        data.oneHide = true;
                                        data.twoHide = true;
                                        tableDatas.push(data);
                                    }
                                });
                            }
                            else {
                                oneLength++;
                            }
                        });
                        oneData.rowSpan = oneLength;
                    });
                    $scope.tableData = tableDatas;
                }
            }
            /**
             * 返回
             */
            $scope.goBack = function() {
                $state.go("drugStoreIntgrityManager.drugStoreIntgrityList");
            };
            /**
             * 考评撤销
             */
            $scope.cancel = function() {
            	var parameter={
                		evaluationCode:$stateParams.evaluationCode
                		};
            		$scope.loading = true;
            		DrugStoreService.cancelIntgrity(parameter).success(function(res){
					$scope.loading = false;
					Modal.alert(res.msg);
				}).error(function(e){
					$scope.loading = false;
					Modal.alert("error");
				});
            };
            
            loadIntgrityEvalData();
            loadIntgrityEvalTypeInfo();
            
        }]);
})();
